package cn.faifai.desktop.events.listener;

import cn.faifai.desktop.events.TreeItemUpdateTagEvent;
import cn.faifai.common.utils.SpringContextUtil;
import cn.faifai.desktop.view.components.treeDirectory.TagBrowser;
import com.google.common.eventbus.Subscribe;
import javafx.scene.control.TreeItem;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Data
@Slf4j
public class TagBrowserUpdateListener {

    @Subscribe
    public void afterTagTreeItemUpadateSussess(TreeItemUpdateTagEvent event) {
        TreeItem targetSelectTreeItem = event.getTreeItemUpdateEvent().getTargetSelectTreeItem();
        TagBrowser tagBrowser = SpringContextUtil.getContext().getBean(TagBrowser.class);
        log.info("需要更新的旧项：{}", targetSelectTreeItem.getValue());
        tagBrowser.loadChildren(targetSelectTreeItem);
        if (event.getTreeItemUpdateEvent().getSelectedTagId() != null){
            TreeItem theParentNodeThatHasBeenReplaced = tagBrowser.searchNonRecursive(event.getTreeItemUpdateEvent().getSelectedTagId());
            if (theParentNodeThatHasBeenReplaced != null && theParentNodeThatHasBeenReplaced != targetSelectTreeItem){
                log.info("需要更新的新项：{}", theParentNodeThatHasBeenReplaced.getValue());
                tagBrowser.loadChildren(theParentNodeThatHasBeenReplaced);
            }
        }
    }
}
